{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 032.longest-valid-parentheses 最长有效括号\n",
    "\n",
    "### 难度：Easy\n",
    "\n",
    "## 刷题内容\n",
    "\n",
    "> 原题链接\n",
    "\n",
    " - 中文：https://leetcode-cn.com/problems/longest-valid-parentheses/description\n",
    " - 英文：https://leetcode.com/problems/longest-valid-parentheses\n",
    " \n",
    "> 内容描述\n",
    "\n",
    "```\n",
    "给定一个只包含 '(' 和 ')' 的字符串，找出最长的包含有效括号的子串的长度。\n",
    "\n",
    "示例 1:\n",
    "输入: \"(()\"\n",
    "输出: 2\n",
    "解释: 最长有效括号子串为 \"()\"\n",
    "\n",
    "示例 2:\n",
    "输入: \")()())\"\n",
    "输出: 4\n",
    "解释: 最长有效括号子串为 \"()()\"\n",
    "```\n",
    "\n",
    "## 解题方案\n",
    "\n",
    "> 思路 1\n",
    "\n",
    "**注：** 千万注意，这个题上来就坑了一下，题目没有说清楚一种情况，比如 (()) ，这种情况，有效括号子串是 4 ，而不是错误的。\n",
    "\n",
    "实际上，在看到这个最长有效括号的题目的时候，我就想到了之前我们做的一个题目。LeetCode 20 题，它的解法是如何使用堆栈判断括号序列是否可以成功配对。我们仍然可以将堆栈的思想延续到这里。\n",
    "\n",
    "每当遇到一个左括号或者是无法成对的右括号，就将它压入栈中，可以成对的括号则从栈中 pop 出。这样栈中剩下的就是无法成对的括号的下标。这时我们可以判断这些下标间的距离来获得最大的成对括号长度。 **在这里，我们需要先遍历一遍字符串，再遍历一下非空的堆栈。一定要注意，这里我们遍历的非空的栈存储的是没有匹配上的括号下标，匹配上的我们都已经做了pop 处理。**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def longestValidParentheses(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        # 创建一个 stack ，用来做 栈 的操作\n",
    "        stack = []\n",
    "        # 第一次遍历 s 字符串\n",
    "        for i in range(len(s)):\n",
    "            # 如果当前循环到的 字符 是 右括号，那就要 检查一下栈内是否有与之匹配的 左括号\n",
    "            if s[i] == ')':\n",
    "                # stack 如果不为空，并且 stack 的栈顶元素存储的下标在 s 字符串中是 左括号\n",
    "                if stack and s[stack[-1]] == '(':\n",
    "                    # 将栈顶元素 pop 出来，与当前的 右括号 配对\n",
    "                    stack.pop()\n",
    "                    # 直接 continue\n",
    "                    continue\n",
    "            stack.append(i)\n",
    "        # 设置 最大长度\n",
    "        max_length = 0\n",
    "        # 设置为当前字符串 s 的长度\n",
    "        next_index = len(s)\n",
    "        # 遍历 非空的栈\n",
    "        while stack:\n",
    "            # 当前的栈顶存储的 s 的下标\n",
    "            cur_index = stack.pop()\n",
    "            # 计算下一个有效的最长括号长度\n",
    "            cur_length = next_index - cur_index - 1\n",
    "            # 将之与 之前存储的 max_length 比较，留下较大值\n",
    "            max_length = max(cur_length, max_length)\n",
    "            # 下一个的有效括号的右索引赋值，接着进入下一次循环，一直到 stack 为空\n",
    "            next_index = cur_index\n",
    "        # 遍历到最后的时候，肯定 next_index 即是有效符号的长度，与 max_length 做比较，取较大值\n",
    "        return max(next_index, max_length)\n",
    "            \n",
    "    \n",
    "s = Solution()\n",
    "print(s.longestValidParentheses(\"()(())\"))\n",
    "print(s.longestValidParentheses(\"()()(()\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 2\n",
    "\n",
    "使用动态规划（dynamic programming）思想来做这个问题。\n",
    "\n",
    "下面是 片刻 大佬想出来的，我感觉思想真的超棒。参考链接：http://www.cnblogs.com/George1994/p/7531574.html\n",
    "\n",
    "1. 用一个 dp 数组来存放以每个 index 为结尾的最长有效括号子串长度，例如：dp[3] = 2 代表以 index 为 3 结尾的最长有效括号子串长度为 2\n",
    "2. 很明显，dp[i] 和 dp[i-1] 之间是有关系的。\n",
    " - 当 dp[i] 所在的索引 i 在字符串 s 中代表的字符是 \"(\" 时，也就是 s[i] == '(' ，很显然，此时的 dp[i] 与之前的一个关系是 dp[i] = dp[i-1] + 0，也就是不用处理。\n",
    " - 当 dp[i] 所在的索引 i 在字符串 s 中代表的字符是 \")\" 时，也就是 s[i] == ')' 时，如果在 dp[i-1] 的所表示的最长有效括号子串之前还有一个 '(' 与 s[i] 对应，那么 dp[i] = dp[i-1] + 2，并且还可以继续往前追溯（如果前面还能连接起来的话）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def longestValidParentheses(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if len(s) == 0:\n",
    "            return 0\n",
    "        dp = [0 for i in range(len(s))]\n",
    "        for i in range(1, len(s)):\n",
    "            if s[i] == ')':\n",
    "                left = i - 1 - dp[i-1]\n",
    "                if left >= 0 and s[left] == '(':\n",
    "                    dp[i] = dp[i-1] + 2\n",
    "                    if left > 0: # 这个是判断 left 前面是否能与后面继续连起来\n",
    "                        dp[i] += dp[left-1]\n",
    "        return max(dp)\n",
    "\n",
    "s = Solution()\n",
    "print(s.longestValidParentheses(\"()(())\"))\n",
    "print(s.longestValidParentheses(\"()()(()\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
